﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 学生成绩管理系统
{
    public partial class Frmaddclass : Form
    {
        private string o_id = null;
        public Frmaddclass()
        {
            InitializeComponent();
        }
        public Frmaddclass(string id)
        {
            InitializeComponent();
            this.o_id = id;
            if (o_id != "")
            {
                ClassInfoData info = new ClassInfoData();
                info.Classid = id;
                DataSet ds = ClassInfoOperation.getClassInfo(info);
                this.txtClass.Text = ds.Tables[0].Rows[0]["班级名称"].ToString();
                this.txtBz.Text = ds.Tables[0].Rows[0]["备注"].ToString();
                this.txtNum.Text = ds.Tables[0].Rows[0]["学生人数"].ToString();
                this.combZy.Text = ds.Tables[0].Rows[0]["专业名称"].ToString();
                this.txtClass.Enabled = false;
                this.Text = "班级信息修改";
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            string classId = this.txtClass.Text;
            string zyId = (string)this.combZy.Text;
            string num = this.txtNum.Text;
            string bz = this.txtBz.Text;
            if (classId == null || classId.Trim().Equals(""))
            {//班级名称不允许为空
                MessageBox.Show("请输入班级名称", "提示");
                this.txtClass.Focus();
                return;
            }
            if (num != null && !num.Trim().Equals(""))
            {//人数不允许为空并且输入的内容位数字
                try
                {
                    int.Parse(num);
                }
                catch (Exception ex)
                {
                    ex.ToString();
                    MessageBox.Show("人数请输入整数！", "提示");
                    this.txtNum.Focus();
                    return;
                }
            }
            ClassInfoData data = new ClassInfoData();
            data.Classid = classId;
            data.Specialtyid = zyId;
            data.Studentnumber = num; data.Remark = bz;
            try
            {
                if (o_id == "")
                {
                    if (ClassInfoOperation.insertClassInfo(data))
                    {
                        MessageBox.Show("修改成功！", "提示");
                        this.txtClass.Text = "";
                        this.txtNum.Text = "";
                        this.txtBz.Text = "";
                    }
                    else
                    {
                        MessageBox.Show("修改失败！", "错误");
                    }
                }
                else
                {
                    if (ClassInfoOperation.updateClassInfo(data))
                    {//修改信息
                        MessageBox.Show("修改成功！", "提示");
                    }
                    else
                    {
                        MessageBox.Show("修改失败!", "错误");
                    }
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
                MessageBox.Show("保存失败！", "错误");
            }
        }

        private void Frmaddclass_Load(object sender, EventArgs e)
        {
            string ConnectionSttring = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
            SqlConnection cn = new SqlConnection(ConnectionSttring);
            cn.Open();
            string str = "select 专业id from 班级信息表 group by 专业id";
            SqlCommand cm = new SqlCommand(str, cn);
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                this.combZy.Items.Add(dr[0]);
            }
            cm.Dispose();
            cn.Close();
            cn.Dispose();
        }

        private void btnCannel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}